package br.com.simon.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import br.com.caelum.vraptor.ioc.Component;
import br.com.simon.modelo.Questao;

@Component
public class QuestaoDao {

	private Session session;

	public QuestaoDao(Session session) {
		this.session = session;
	}

	public void salva(Questao questao) {
		Transaction transaction = session.beginTransaction();
		session.save(questao);
		transaction.commit();
	}

	@SuppressWarnings("unchecked")
	public List<Questao> listaTudo() {
		Criteria criteria = session.createCriteria(Questao.class);
		return criteria.list();
	}

	public Questao getQuestao(int id) {
		return (Questao) session.load(Questao.class, id);
	}

	public void atualiza(Questao questao) {
		Transaction transaction = session.beginTransaction();
		session.update(questao);
		transaction.commit();
	}

	public void remove(Questao questao) {
		Transaction transaction = session.beginTransaction();
		session.delete(questao);
		transaction.commit();
	}

	@SuppressWarnings("unchecked")
	public List<Questao> busca(String id) {
		Criteria criteria = session.createCriteria(Questao.class);
		criteria.add(Restrictions.ilike("id", id, MatchMode.ANYWHERE));
		return criteria.list();
	}

	public void recarrega(Questao questao) {
		session.refresh(questao);
	}
}
